import * as THREE from 'three';

class OBJExporter {
    parse(object) {
        let output = '';
        let indexVertex = 0;
        let indexVertexUvs = 0;
        let indexNormals = 0;

        // 顶点、UV和法线数据
        const vertex = new THREE.Vector3();
        const normal = new THREE.Vector3();
        const uv = new THREE.Vector2();
        const face = [];

        output += 'o exported\n';

        // 解析网格
        object.traverse(function (child) {
            if (child.isMesh) {
                const geometry = child.geometry;
                const matrixWorld = child.matrixWorld;

                if (geometry.isBufferGeometry) {
                    const vertices = geometry.getAttribute('position');
                    const normals = geometry.getAttribute('normal');
                    const uvs = geometry.getAttribute('uv');
                    const indices = geometry.getIndex();

                    // 顶点
                    for (let i = 0, l = vertices.count; i < l; i++) {
                        vertex.x = vertices.getX(i);
                        vertex.y = vertices.getY(i);
                        vertex.z = vertices.getZ(i);
                        vertex.applyMatrix4(matrixWorld);

                        output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
                    }

                    // UV
                    if (uvs !== undefined) {
                        for (let i = 0, l = uvs.count; i < l; i++) {
                            uv.x = uvs.getX(i);
                            uv.y = uvs.getY(i);

                            output += 'vt ' + uv.x + ' ' + uv.y + '\n';
                        }
                    }

                    // 法线
                    if (normals !== undefined) {
                        for (let i = 0, l = normals.count; i < l; i++) {
                            normal.x = normals.getX(i);
                            normal.y = normals.getY(i);
                            normal.z = normals.getZ(i);
                            normal.applyMatrix4(matrixWorld).normalize();

                            output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
                        }
                    }

                    // 面
                    output += 'usemtl material\n';

                    if (indices !== null) {
                        for (let i = 0, l = indices.count; i < l; i += 3) {
                            for (let j = 0; j < 3; j++) {
                                const idx = indices.getX(i + j) + 1;
                                output += 'f ' + (idx + indexVertex) + '/' + (uvs ? (idx + indexVertexUvs) : '') + '/' + (normals ? (idx + indexNormals) : '') + ' ';
                            }
                            output += '\n';
                        }
                    } else {
                        for (let i = 0, l = vertices.count; i < l; i += 3) {
                            for (let j = 0; j < 3; j++) {
                                const idx = i + j + 1;
                                output += 'f ' + (idx + indexVertex) + '/' + (uvs ? (idx + indexVertexUvs) : '') + '/' + (normals ? (idx + indexNormals) : '') + ' ';
                            }
                            output += '\n';
                        }
                    }

                    // 更新偏移
                    indexVertex += vertices.count;
                    if (uvs) indexVertexUvs += uvs.count;
                    if (normals) indexNormals += normals.count;
                }
            }
        });

        return output;
    }
}

export { OBJExporter }; 